package com.wgchao.pharmacymg.Infrastructure.exception;

import com.wgchao.pharmacymg.Infrastructure.back.ResultCommon;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: GlobalExceptionHandler
 * @author: wgchao
 * @createTime: 2019/9/3 11:12 AM
 * @Description: 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultCommon exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
        return handleErrorInfo(request, exception.getMessage(), exception);
    }

    private ResultCommon<String> handleErrorInfo(HttpServletRequest request, String message, Exception exception) {
        exception.printStackTrace();
        ResultCommon rc = ResultCommon.fail(message);
        if(exception instanceof CommonException){
            rc.setCode(((CommonException) exception).getCode());
        }
        return rc;
    }
}
